package com.blockchain.console.servlets.financial.bzc;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.common.enums.XlbZcStatus;
import com.blockchain.common.enums.ZrType;
import com.blockchain.console.service.financial.BManage;
import com.blockchain.console.service.financial.entity.BEntity;
import com.blockchain.console.service.financial.query.BQuery;
import com.blockchain.console.servlets.financial.AbstractFinancialServlet;
import com.blockchain.framework.http.servlet.annotation.Right;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.query.Paging;
import com.blockchain.framework.service.query.PagingResult;
import com.blockchain.util.parser.EnumParser;
import com.blockchain.util.parser.IntegerParser;
import com.blockchain.util.parser.TimestampParser;

@Right(id = "C_FINANCIAL_EXPORBZC", name = "财务管理-虚拟币转出-导出")
public class ExportBzc extends AbstractFinancialServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void processGet(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		processPost(request, response, serviceSession);
	}

	@Override
	protected void processPost(final HttpServletRequest request, final HttpServletResponse response,
			final ServiceSession serviceSession) throws Throwable {
		response.setHeader("Content-disposition",
				"attachment;filename=" + new Timestamp(System.currentTimeMillis()).getTime() + ".csv");
		response.setContentType("application/csv");
		XlbZcStatus zt = EnumParser.parse(XlbZcStatus.class, request.getParameter("zczt"));
		if (zt == null) {
			zt = XlbZcStatus.WSH;
		}
		BManage extraction = serviceSession.getService(BManage.class);
		PagingResult<BEntity> result = extraction.searchBzc(new BQuery() {

			@Override
			public ZrType getZt() {
				return EnumParser.parse(ZrType.class, request.getParameter("zt"));
			}

			@Override
			public String getYhm() {
				return request.getParameter("yhm");
			}

			@Override
			public int getUserid() {
				return IntegerParser.parse(request.getParameter("userid"));
			}

			@Override
			public Timestamp getStartExtractionTime() {
				return TimestampParser.parse(request.getParameter("s"));
			}

			@Override
			public Timestamp getEndExtractionTime() {
				return TimestampParser.parse(request.getParameter("e"));
			}

			@Override
			public int getBid() {
				return IntegerParser.parse(request.getParameter("bid"));
			}

			@Override
			public XlbZcStatus getXlbZt() {
				XlbZcStatus zt = EnumParser.parse(XlbZcStatus.class, request.getParameter("zczt"));
				if (zt == null) {
					zt = XlbZcStatus.WSH;
				}
				return zt;
			}
		}, new Paging() {
			@Override
			public int getSize() {
				return Integer.MAX_VALUE;

			}

			@Override
			public int getCurrentPage() {
				return IntegerParser.parse(request.getParameter(PAGING_CURRENT));

			}
		});
		extraction.export(result.getItems(), response.getOutputStream(), "", zt);
	}
}
